/**
 * @file 报表门户API
 * @author liushuang(liushuang04@baijia.com)
 * @date 2019-11-15
 */

import axios from 'axios';
import moment from 'moment-timezone';
import {BaseUrl} from '~/utils/common/envType';
import http from './http';
import constant from './constant';

const instance = axios.create({
    // timeout: 3000,
    retry: 3,
    retryDelay: 1000,
    withCredentials: true,
    responseType: 'blob'
});

export const getReportIdList = pageId => http.post(constant.REPORT.GET_REPORTLIST, {id: pageId});

/**
 * 报表门户 获取菜单
 * API介绍地址：http://api.baijia.com/project/403/interface/api/90081
 * url: /api/portal/menu
 * 方法：POST
 *
 * @param  无参数
 * @return {Promise}
 */

export const getMenuList = params => http.post(constant.REPORT.GET_MENU, params);

/**
 * 报表管理 获取报表详情
 * API介绍地址：http://api.baijia.com/project/403/interface/api/89321
 * url: /api/portal/detail
 * 方法：POST
 *
 * @param  {String} id   报表id
 * @return {Promise}
 */
export const getReportInfo = params => http.post(constant.REPORT.GET_REPORT_INFO, params);


/**
 * 选择维度（下拉框）
 * API介绍地址：http://api.baijia.com/project/403/interface/api/89313
 * url: /api/portal/select
 * 方法：POST
 *
 * @param  {Object} params   API需要的参数
 * @return {Promise}
 */

export const fetchSelection = (params, cancelToken) => http.post(
    constant.REPORT.FETCH_SELECTION, params, {cancelToken}
);

/**
 * echart获取显示
 * API介绍地址：http://api.baijia.com/project/403/interface/api/89893
 * url: /api/portal/map
 * 方法：POST
 *
 * @param  {Object} params   API需要的参数
 * @return {Promise}
 */

// export const getDataMap = params => http.post(constant.REPORT.GET_DATA_MAP, params);

/**
 * 查询data
 * API介绍地址：http://api.baijia.com/project/403/interface/api/89481
 * url: /api/portal/search
 * 方法：POST
 *
 * @param  {Object} params   API需要的参数
 * @return {Promise}
 */

export const fetchReports = params => http.post(constant.REPORT.FETCH_REPORTS, params);

/**
 * 报表下载
 * API介绍地址：http://api.baijia.com/project/403/interface/api/89505
 * url: /api/portal/download
 * 方法：POST
 *
 * @param  {Object} params   API需要的参数
 * @return {Promise}
 */

export const downloadReports = (params, name, callback) => {
    const today = moment().format('YYYYMMDDHHmmss');

    const promise = new Promise((resolve, reject) => {
        instance
            .post(`${BaseUrl}${constant.REPORT.DOWNLOAD_REPORTS}`, params)
            .then(res => {
                // application/vnd.openxmlformats-officedocument.spreadsheetml.sheet这里表示xlsx类型
                const blob = new Blob([res.data], {
                    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
                });
                const downloadElement = document.createElement('a');
                const href = window.URL.createObjectURL(blob);
                downloadElement.href = href;
                downloadElement.download = `${name}_${today}.xlsx`;
                document.body.appendChild(downloadElement);
                downloadElement.click();
                document.body.removeChild(downloadElement);
                window.URL.revokeObjectURL(href);
                callback();
            })
            .catch(error => {
                reject(error);
            });
    });

    return promise;
};
